package sz.szsmk.citizencard.ui.newversion.fragment.adapter

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Rect
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import kotlinx.android.synthetic.main.layout_lifehappyplay.view.*
import sz.szsmk.citizencard.DaggerApplication
import sz.szsmk.citizencard.R
import sz.szsmk.citizencard.model.network.clarkbean.response.AdInfo
import sz.szsmk.citizencard.util.ACache
import sz.szsmk.citizencard.util.Constant
import sz.szsmk.citizencard.util.GlideRoundTransform
import sz.szsmk.citizencard.util.JumpUtils

/**
 *  author : hyt
 *  date : 2020/11/12
 *  description :
 */
class LifeHappyPlayAdapter(var mContext: Context? = null)
    : RecyclerView.Adapter<LifeHappyPlayAdapter.HappyPlayHolder>(){
    var datas = mutableListOf<AdInfo>()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
             HappyPlayHolder(LayoutInflater.from(mContext)
                     .inflate(R.layout.layout_lifehappyplay, parent, false))

    override fun onBindViewHolder(holder: HappyPlayHolder, position: Int) {
        var temp = datas[position]
        Glide.with(mContext).load(temp.resource_url)
                .transform(GlideRoundTransform(mContext))
                .into(holder.itemView.iv_img)

        holder.itemView.tv_title.text = temp.resource_name
        holder.itemView.tv_subTitle.text = temp.resource_description

        holder.itemView.btn_yuyue.setOnClickListener {
            val bundle = Bundle()
            bundle.putString("url", Constant.BASE_URL + "app-front-end/#/card/spotBook?spot_id=${temp.resource_id}&access_token=" +
                    ACache.get(DaggerApplication.getIns()).getAsString("access_token"))
            JumpUtils.jumptoNatvie(mContext, "webView", 1, bundle)
        }

    }
    override fun getItemCount() = datas.size

    fun addDatas(datas: List<AdInfo>){
        this.datas.clear()
        this.datas.addAll(datas)
        notifyDataSetChanged()
    }

    class HappyPlayHolder(var view: View) : RecyclerView.ViewHolder(view)

}

class LifeHappyPlayDecoration(var context: Context) :  DividerItemDecoration(context, VERTICAL){

    private var mPaint: Paint? = null
    private var mDivider: Drawable? = null
    private val mDividerHeight = 1 //分割线高度，默认为1px

    private val mOrientation //列表的方向：LinearLayoutManager.VERTICAL或LinearLayoutManager.HORIZONTAL
            = 0
    private val ATTRS = intArrayOf(android.R.attr.listDivider) //使用系统自带的listDivider

    init {

        var a = context.obtainStyledAttributes(ATTRS);//使用TypeArray加载该系统资源
        mDivider = a.getDrawable(0)
        a.recycle();//缓存

        mPaint =   Paint(Paint.ANTI_ALIAS_FLAG)
        mPaint?.color = ContextCompat.getColor(context, R.color.color_f3f3f3)
        mPaint?.style = Paint.Style.FILL
    }

    override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        super.onDraw(c, parent, state)
        drawHorizontal(c, parent)
    }

    //获取分割线尺寸
    override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
        super.getItemOffsets(outRect, view, parent, state)
        //设置偏移的高度是mDivider.getIntrinsicHeight，该高度正是分割线的高度
        outRect.set(0, 0, 0, mDividerHeight)
    }

    //绘制横向 item 分割线
    private fun drawHorizontal(canvas: Canvas, parent: RecyclerView) {
        val left = parent.paddingLeft //获取分割线的左边距，即RecyclerView的padding值
        val right = parent.measuredWidth - parent.paddingRight //分割线右边距
        val childSize = parent.childCount
        //遍历所有item view，为它们的下方绘制分割线
        //需求 最后一个没有分割线
        for (i in 0 until childSize-2) {
            val child = parent.getChildAt(i)
            val layoutParams = child.layoutParams as RecyclerView.LayoutParams
            val top = child.bottom + layoutParams.bottomMargin
            val bottom: Int = top + mDividerHeight
            if (mDivider != null) {
                mDivider!!.setBounds(left - 20, top, right, bottom)
                mDivider!!.draw(canvas)
            }
            if (mPaint != null) {
                canvas.drawRect(left.toFloat() - 20, top.toFloat(), right.toFloat(), bottom.toFloat(), mPaint!!)
            }
        }
    }



}